﻿using Miao.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// MiaoUI预制的快速公用件
/// </summary>
public static class UIPresetUtility
{
    #region 提示框
    public static async Task ShowToast(string label)
    {
        await ShowToast(label, 1);
    }
    /// <summary> 显示一个提示 </summary>
    /// <param name="label">显示文本</param>
    /// <param name="duration">延时</param>
    /// <param name="type">type</param>
    public static async Task ShowToast(string label, float delayTime, float fontSize = 36)
    {
        var toast = await UIManager.OpenAsync<Toast>();
        toast.delayTime = delayTime;
        toast.fontSize = fontSize;
        toast.label = label;
        toast.Play();
    }
    #endregion
    #region 对话框
    public static async Task<RoleDialog> ShowRoleDialog(Sprite roleHead, string roleName, string[] text)
    {
        var dialog = await UIManager.OpenAsync<RoleDialog>();
        dialog.textQuantity = text;
        dialog.roleHead.sprite = roleHead;
        dialog.roleName.text = roleName;
        return dialog;
    }
    #endregion
    #region 选择框
    public static async Task ShowSelect(string title, string text, UnityAction yesCall, UnityAction noCall)
    {
        await ShowSelect(title, text,"是","否", yesCall, noCall,null);
    }

    public static async Task ShowSelect(string title, string text,string yesText,string noText, UnityAction yesCall, UnityAction noCall,Func<bool> yesCondition)
    {
        var select = await UIManager.OpenAsync<Select>();
        select.title.text = title;
        select.content.text = text;
        select.yesText.text = yesText;
        select.noText.text = noText;
        select.yes.onClick.AddListener(yesCall);
        select.yes.onClick.AddListener(select.Close);
        select.no.onClick.AddListener(noCall);
        select.no.onClick.AddListener(select.Close);
        select.Condition = yesCondition;
    }

    public static async Task<Select> ShowSelect(string title, string text, string yesText, string noText, UnityAction yesCall, UnityAction noCall, Func<bool> yesCondition, BaseUIMotion show, BaseUIMotion close)
    {
        var select = await UIManager.OpenMotionAsync<Select>(show,close);
        select.InitSelect(title, text, yesText, noText, yesCall, noCall, yesCondition);
        return select;
    }
    private static void InitSelect(this Select select, string title, string text, string yesText, string noText, UnityAction yesCall, UnityAction noCall, Func<bool> yesCondition)
    {
        select.title.text = title;
        select.content.text = text;
        select.yesText.text = yesText;
        select.noText.text = noText;
        select.yes.onClick.AddListener(yesCall);
        select.no.onClick.AddListener(noCall);
        select.Condition = yesCondition;
    }


    #endregion

    #region 弹窗
    public static async Task ShowPopup(string title, string text)
    {
        var popup = await UIManager.OpenAsync<Popup>();
        popup.title.text = title;
        popup.content.text = text;

    }
    #endregion
}

